package com.android.bonn.fast.widget.view;

import android.view.View;
import android.view.ViewTreeObserver;

import androidx.core.widget.NestedScrollView;

/**
 * author huangyunlin@snqu.com
 * date 2018/12/14
 * description
 */
public abstract class OnNestedScrollViewChangeListener implements NestedScrollView.OnScrollChangeListener {

    private int color;
    private int headerTranslateY = 0;
    private int headerMinTranslate;
    private int headerViewHeight = 0;
    private int headerMaxTranslate = 0;
    private View headerView;

    public OnNestedScrollViewChangeListener(int color, View headerView) {
        this.color = color;
        setHeaderView(headerView);
    }

    public OnNestedScrollViewChangeListener(int color) {
        this.color = color;
    }


    private void setHeaderView(final View view) {
        if (view.getHeight() == 0) {
            view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    headerViewHeight = view.getHeight();
                    headerMinTranslate = -headerViewHeight - 40;
                }
            });
        } else {
            headerViewHeight = view.getHeight();
            headerMinTranslate = -headerViewHeight - 40;
        }
        this.headerView = view;
        headerMaxTranslate = 0;
    }


    private int computeViewTranslateY(NestedScrollView nestedScrollView, int scrollY, int oldScrollY) {
        int headerDelta = oldScrollY - scrollY;

        if (headerView != null) {
            // Going up
            if (headerDelta < 0 && headerTranslateY > headerMinTranslate) {
                headerTranslateY = headerTranslateY + headerDelta;
                if (headerTranslateY < headerMinTranslate)
                    headerTranslateY = headerMinTranslate;
            }

            // Going down
            if (headerDelta > 0 && headerTranslateY < headerMaxTranslate) {
                if (scrollY < headerMaxTranslate)
                    headerDelta *= 3;
                headerTranslateY = headerTranslateY + headerDelta;
                if (scrollY < 5)
                    headerTranslateY = headerMaxTranslate;
                if (headerTranslateY > headerMaxTranslate)
                    headerTranslateY = headerMaxTranslate;

            }
            return headerTranslateY;
        }
        return headerDelta;
    }

    @Override
    public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        int temp = color;
        if (scrollY < 256) {
            int alpha = (scrollY << 24) | (-1 >>> 8);
            temp = color & (alpha);
        }

        int offset = computeViewTranslateY(nestedScrollView, scrollY, oldScrollY);

        if (headerView != null) {
            headerView.setTranslationY(offset);
            //nestedScrollView.setTranslationY(offset);
        }

        onChange(nestedScrollView, temp, scrollX, scrollY, oldScrollX, oldScrollY, oldScrollY - scrollY);

    }

    public abstract void onChange(NestedScrollView nestedScrollView, int color, int scrollX, int scrollY, int oldScrollX, int oldScrollY, int offset);
}
